package com.amap.api.col.s;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import com.amap.api.col.s.cf;
import com.amap.api.services.core.AMapException;
import com.amap.api.services.interfaces.IDistanceSearch;
import com.amap.api.services.route.DistanceResult;
import com.amap.api.services.route.DistanceSearch;
import com.tencent.smtt.sdk.TbsListener;

/* compiled from: DistanceSearchCore.java */
/* loaded from: classes.dex */
public class bj implements IDistanceSearch {
    private static final String a = "bj";
    private Context b;
    private Handler c;
    private DistanceSearch.OnDistanceSearchListener d;

    public bj(Context context) throws AMapException {
        cg a2 = cf.a(context, m.a(false));
        if (a2.a != cf.c.SuccessCode) {
            throw new AMapException(a2.b, 1, a2.b, a2.a.a());
        }
        this.b = context.getApplicationContext();
        this.c = y.a();
    }

    @Override // com.amap.api.services.interfaces.IDistanceSearch
    public DistanceResult calculateRouteDistance(DistanceSearch.DistanceQuery distanceQuery) throws AMapException {
        try {
            w.a(this.b);
            if (distanceQuery == null) {
                throw new AMapException(AMapException.AMAP_CLIENT_INVALID_PARAMETER);
            }
            if (a(distanceQuery)) {
                throw new AMapException(AMapException.AMAP_CLIENT_INVALID_PARAMETER);
            }
            DistanceSearch.DistanceQuery m21clone = distanceQuery.m21clone();
            DistanceResult c = new o(this.b, m21clone).c();
            if (c != null) {
                c.setDistanceQuery(m21clone);
            }
            return c;
        } catch (AMapException e) {
            n.a(e, a, "calculateWalkRoute");
            throw e;
        }
    }

    private static boolean a(DistanceSearch.DistanceQuery distanceQuery) {
        return distanceQuery.getDestination() == null || distanceQuery.getOrigins() == null || distanceQuery.getOrigins().size() <= 0;
    }

    @Override // com.amap.api.services.interfaces.IDistanceSearch
    public void calculateRouteDistanceAsyn(final DistanceSearch.DistanceQuery distanceQuery) {
        ax.a().a(new Runnable() { // from class: com.amap.api.col.s.bj.1
            {
                bj.this = this;
            }

            @Override // java.lang.Runnable
            public final void run() {
                Message obtainMessage = y.a().obtainMessage();
                obtainMessage.what = TbsListener.ErrorCode.INFO_CODE_BASE;
                obtainMessage.arg1 = 16;
                Bundle bundle = new Bundle();
                DistanceResult distanceResult = null;
                try {
                    try {
                        distanceResult = bj.this.calculateRouteDistance(distanceQuery);
                        bundle.putInt("errorCode", 1000);
                    } catch (AMapException e) {
                        bundle.putInt("errorCode", e.getErrorCode());
                    }
                } finally {
                    obtainMessage.obj = bj.this.d;
                    bundle.putParcelable("result", distanceResult);
                    obtainMessage.setData(bundle);
                    bj.this.c.sendMessage(obtainMessage);
                }
            }
        });
    }

    @Override // com.amap.api.services.interfaces.IDistanceSearch
    public void setDistanceSearchListener(DistanceSearch.OnDistanceSearchListener onDistanceSearchListener) {
        this.d = onDistanceSearchListener;
    }
}
